<template>
    <view >
        <div v-if="total > 0" class="carpage">
            <div class="good_list">
                <div class="parts">
                    <div class="goods_item" v-for="(item, idx) in listArr" :key="idx">
                        <div class="selectBox" @click="goSelect(item)">
                            <uni-icons :type="item.check ? 'checkbox-filled' : 'circle'" size="30" :color="item.check ? color : '#c1c1c1'"></uni-icons>
                        </div>

                        <div class="delBox" @click="goDel(item)">
                            <!-- <uni-icons type="closeempty" size="30" color="#c1c1c1" ></uni-icons> -->
                            <span class="iconfont icon-guanbi1"></span>
                        </div>

                        <div class="goods_info">
                            <div class="imgbox" @click.stop="$goNext('/pages/supermarket/product/index?id=' + item.goodsId)">
                                <img :src="$getImg(item.specificationsFileId)" mode="aspectFit" />
                            </div>
                            <div class="r-box">
                                <div class="name text-delic">
                                    <span class="">{{ item.goodsName }}</span>
                                </div>

                                <div class="sbu_name text-delic" @click.stop="styleClick(item)">
                                    <span class="l text-delic">{{ item.specificationsName }}</span>
                                    <span class="iconfont icon-xiangxia"></span>
                                </div>

                                <div class="price-box">
                                    <div>
                                        <text class="">￥</text>
                                        <text class="big b">{{ item.payMoney }}</text>
                                    </div>
                                    <CountHui :numVal.sync="item.goodsNum" @change="productChange" :parameter="item.id" :limit="1" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="fixed-foot" :class="{ hasSafe: hasSafe }">
                <div class="foot-Box">
                    <div class="b-l">
                        <div class="selectBox" @click="goSelectAll">
                            <uni-icons :type="isAll ? 'checkbox-filled' : 'circle'" size="30" :color="isAll ? color : '#c1c1c1'"></uni-icons>
                            <span>全选</span>
                        </div>
                    </div>
                    <div class="b-r">
                        <div class="price">
                            <div class="p-top">
                                <span>共计:</span>
                                <span class="p b">
                                    <span class="f24">￥</span>
                                    {{ totalMoney }}
                                </span>
                            </div>
                            <!-- <div class="p-bottom">
                                <span>总计:</span> <span class="color-col">￥231</span>优惠￥0
                            </div> -->
                        </div>
                        <div class="r-btn">
                            <div class="btn" :class="{ isHui: lenth == 0 }" @click="goPayOrder">
                                <span>去结算({{ lenth }})</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div v-else>
            <div class="emptBox">
                <img src="http://192.168.8.10:8700/sex_imgs/app/buy-car.png" class="eptImg" mode="aspectFit" srcset="" />
                <div class="span">购物车还是空的，快去逛逛吧~</div>

                <div class="openBtn transBg5" @click="$goNext('/pages/list/index')">
                    <span>去逛逛</span>
                </div>

            </div>
        </div>

        <BuyPopup ref="BuyPopup" :styleArr="styleArr" @chooseFinish="chooseGoodsFinish" />
        <LoadMask v-if="initMask" :showMask="false" />
    </view>
</template>

<script>
    import { GoodsCarList, GoodsCarEdit, GoodsCarCountNums, GoodsCarUserMoney, GoodsCarIdsMoney, GoodsCarDel, PpecificationsList } from '@/api/http';
    import CountHui from '@/components/Count/hui';
    import BuyPopup from '@/components/BuyPopup';
    const app = getApp();
    export default {
        components: { CountHui, CountHui, BuyPopup },
        props: {
            hasSafe: {
                // 是否有安全区
                type: Boolean,
                default: true
            },
            total: {
                // 是否有安全区
                type: Number | String,
                default: 0
            },
        },
        data() {
            return {
                initMask: false,
                form: {
                    pageNum: 1,
                    pageSize: 1000,
                    userId: undefined
                },
                listArr: [],
                color: '#ea4745', //#ea4745
                lenth: 0,
                isAll: false, //是否全选
                totalMoney: 0,
                //当前的规格
                styleArr: [],
                //准备修改的
                selectOne: {}
            };
        },

        mounted(option) {
            // 获取当前页面栈数组
            // const pages = getCurrentPages();
            // // 获取数组中最后一个元素，即当前页面
            // const currentPage = pages[pages.length - 1];
            // // 获取当前页面的路径
            // const currentPath = currentPage.route;
            // console.log('当前页面路径：' + currentPath);
            // if( currentPath == 'pages/carpage/index'){
            // }
        },
        methods: {
            initCar() {
                this.initMask = true;

                let userId = app.globalData.userId;

                this.form.userId = userId;

                GoodsCarList(this.form)
                    .then(res => {
                        if (res.code == 200) {
                            this.listArr = res.data.map(item => {
                                item.check = true;

                                return item;
                            });

                            this.lenth = res.total;

                            this.$emit('update:total', res.total);

                            this.isAll = true;

                            this.singgeCount();
                        }
                    })
                    .finally(() => {
                        this.initMask = false;
                    });
                
            },
            //删除
            goDel(item) {
                let that = this;

                uni.showModal({
                    title: '温馨提示',
                    content: '是否删除此商品',
                    success: function (res) {
                        if (res.confirm) {
                            GoodsCarDel([item.id]).then(res => {
                                that.initCar();
                            });
                        }
                    }
                });
            },
            //加减 当前数  id  +1或-1
            productChange(num, listId, e) {
                let json = null;

                this.listArr.map(item => {
                    if (item.id == listId) {
                        this.$set(item, 'check', true);
                        json = item;
                        // console.log('当前',item)
                    }
                });

                this.isFullAllFunc();

                GoodsCarCountNums({ id: json.id, num: e }).then(res => {
                    // console.log('当前',res)

                    this.$set(json, 'payMoney', res.data.payMoney);

                    this.singgeCount();
                });
            },
            //是否全选
            goSelectAll() {
                this.isAll = !this.isAll;

                this.listArr.map(item => {
                    this.$set(item, 'check', this.isAll);
                });

                this.singgeCount();
            },
            //单选
            goSelect(item) {
                this.$set(item, 'check', !item.check);

                this.isFullAllFunc();

                this.singgeCount();
            },
            isFullAllFunc() {
                let bool = true;

                this.listArr.map(i => {
                    if (!i.check) {
                        bool = false;
                    }
                });

                this.isAll = bool;
            },
            //选了后重新计算价格
            singgeCount() {
                let ids = this.listArr.filter(i => i.check).map(i => i.id);

                // console.log('11111111111111111',ids)

                this.lenth = ids.length;

                if (ids.length == 0) {
                    this.totalMoney = 0;

                    return;
                }

                this.initMask = true;

                GoodsCarIdsMoney(ids)
                    .then(res => {
                        // console.log('计算价格',res)
                        if (res.code == 200) {
                            this.totalMoney = res.data;
                        }
                    })
                    .finally(() => {
                        this.initMask = false;
                    });
            },
            //选择规格
            styleClick(itemRow) {
                this.selectOne = itemRow;

                PpecificationsList({ goodsId: itemRow.goodsId, pageNum: '1', pageSize: 100000 }).then(res => {
                    this.styleArr = res.data.map(item => {
                        item.fileIdPic = this.$getImg(item.fileId);

                        return item;
                    });

                    this.$refs['BuyPopup'].showInitFunc('car', itemRow.specificationsId, itemRow.goodsNum);
                });
            },
            //选择完后
            chooseGoodsFinish(option) {
                let { type, styleId, num } = option;

                // if(type == 'buy'){
                //     this.$goNext('/pages/supermarket/order/index?styleId=' + styleId + '&num=' + num );
                // }
                // else if(type == 'car'){
                //     this.goJoinCar( option  )
                // }

                GoodsCarEdit({
                    id: this.selectOne.id,
                    goodsId: this.selectOne.goodsId,
                    goodsNum: this.selectOne.goodsNum,
                    specificationsId: styleId
                }).then(res => {
                    this.$refs['BuyPopup'].closeBox();

                    // console.log('添加到购物车数量',res)

                    if (res.code == 200) {
                        this.initCar();
                    }
                });
            },
            //去支付
            goPayOrder() {
                if (this.lenth == 0) return;

                let ids = this.listArr
                    .filter(i => i.check)
                    .map(i => i.id)
                    .join(',');

                this.$goNext('/pages/supermarket/order/index?styleId=' + ids);
            }
        }
    };
</script>

<style scoped lang="scss">
    $pageColor: #ea4745;
    .carpage {
        min-height: 100vh;
        padding-bottom: 60px;
    }
    .good_list {
        padding: 0rpx 20rpx 60rpx;
        .parts {
            padding: 30rpx 16rpx 24rpx;
        }
        .goods_item {
            position: relative;
            padding: 20rpx 0 20rpx 80rpx;
            .selectBox {
                position: absolute;
                top: 40%;
                // transform: translateY(-50%);
                left: 0;
            }
            .delBox {
                position: absolute;
                top: 0rpx;
                // transform: translateY(-50%);
                right: 0;
                padding: 10rpx 10rpx 0 4rpx;
                font-size: 26rpx;
                color: #c1c1c1;
            }
        }
        .goods_info {
            display: flex;
            // padding-bottom: 40rpx;
            height: 210rpx;
            .imgbox {
                width: 200rpx;
                height: 200rpx;
                border-radius: 10rpx;
                overflow: hidden;
                img {
                    width: 100%;
                    height: 100%;
                }
            }
            .r-box {
                padding-left: 40rpx;
                flex: 1;
                overflow: hidden;
                .price-box {
                    color: $pageColor;
                    padding: 16rpx 0 20rpx;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    .big {
                        font-size: 38rpx;
                    }
                }
                .name {
                    font-size: 26rpx;
                    width: calc(100% - 80rpx);
                }
                .sbu_name {
                    width: calc(100% - 80rpx);
                    background: rgb(248 248 248);
                    padding: 8rpx 10rpx;
                    line-height: 1.2;
                    border-radius: 10rpx;
                    color: #777;
                    margin: 10px 0;
                    font-size: 24rpx;
                    display: flex;
                    .l {
                        flex: 1;
                    }
                }
            }
        }
    }

    .fixed-foot {
        position: fixed;
        left: 0;
        bottom: 0;
        width: 100%;
        background: #fff;
        &.hasSafe {
            padding-bottom: var(--status-bar-height);
        }
        .foot-Box {
            height: 50px;
            width: 100%;
            padding: 10rpx 30rpx;
            // box-sizing: content-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .selectBox {
            display: flex;
            align-items: center;
            font-size: 28rpx;
            color: #565656;
        }
        .b-r {
            font-size: 28rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
            .price {
                .b {
                    color: $pageColor;
                    font-size: 32rpx;
                }
            }
            .r-btn {
                height: 100%;
                margin-left: 10px;
                .btn {
                    height: 100%;
                    width: 252rpx;
                    font-size: 28rpx;
                    color: #fff;
                    background: $pageColor;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    border-radius: 100rpx;
                    &.isHui {
                        background: #eaeaea;
                        color: #565656;
                    }
                }
            }
        }
    }
    .emptBox {
        text-align: center;
        padding: 40px 0 50px;
        .eptImg {
            width: 110px;
            height: 110px;
        }
        .span {
            // margin-top: 10px;
            color: #c6cacf !important;
            font-size: 30rpx;
        }
        .openBtn {
            padding: 0;
            height: 80rpx;
            line-height: 80rpx;
            text-align: center;
            font-size: 30rpx;
            margin: 20px auto 0;
            border-radius: 60rpx;
            color: #fff;
            width: 50%;
        }
    }
</style>
